//
//  UIViewController+mcrmBizBaseViewController.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15/8/16.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "UIViewController+mcrmBizBaseViewController.h"

@implementation UIViewController (mcrmBizBaseViewController)

-(void)setBackNavigateItemTitle:(NSString*)title
{
    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    self.navTitle = title;
    
    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);
}

-(void)setNavigateItemTitle:(NSString*)title rightItemImageName:(NSString*)imageName
{
    self.navTitle = title;
    [self createRightBarButtonItemWithImage:[UIImage imageNamed:imageName]];
}

-(void)setNavigateItemTitle:(NSString*)title
{
    self.navTitle = title;
}

-(void)setBackNavigateItemTitle:(NSString*)title rightItemText:(NSString*)text
{
    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    self.navTitle = title;
    [self createRightBarButtonItemWithTitle:text];
    
    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);
}

-(void)initBackAndRightItemImageName:(NSString*)imageName andNavigationBarTitle:(NSString *)title
{
    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    self.navTitle = title;
    [self createRightBarButtonItemWithImage:[UIImage imageNamed:imageName]];
    
    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);
}

-(void)setBackNavigateItemRightItemImageName:(NSString*)imageName
{
    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    [self createRightBarButtonItemWithImage:[UIImage imageNamed:imageName]];
    
    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);
}

-(void)setNavigateItemRightItemImageName:(NSString*)imageName
{
    [self createRightBarButtonItemWithImage:[UIImage imageNamed:imageName]];
}

-(void)setBackNavigateItemTitle:(NSString*)title rightItemCustomView:(UIView*)customerView
{
    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    self.navTitle = title;
    [self createRightBarButtonItemWithView:customerView];
    
    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);
}

-(void)setBackNavigateItemTitle:(NSString*)title rightItemImageName:(NSString*)imageName
{
    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
    self.navTitle = title;
    [self createRightBarButtonItemWithImage:[UIImage imageNamed:imageName]];
    
    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);
}

-(void)clickedBackBarButton:(id)sender
{
    [XUINavigationController pop];
}

//-(void)setBackAndSystemItemAddNavigateItemTitle:(NSString*)title
//{
//    [self createLeftBarButtonItemWithImage:[UIImage imageNamed:@"nav_leftbtn_back"]];
//    self.navTitle = title;
//    [self createRightBarButtonItemWithBarButtonSystemItem:UIBarButtonSystemItemAdd];
//    
//    self.leftBarButtonItem.action = @selector(clickedBackBarButton:);    
//}

-(void)setEmptyTipView:(EmptyTipView*)emptyTipView
{
    [self setAssociatedRetainNonatomicValue:emptyTipView withKey:"emptytipview"];
}

-(EmptyTipView*)emptyTipView
{
    return [self associatedValueForKey:"emptytipview"];
}

-(void)showTableViewEmptyTipWithTipType:(EmptyTipType)tip
{
    if([self isKindOfClass:[XUITableViewController class]] && [self emptyTipView] == nil)
    {
        XUITableViewController *vcl = (XUITableViewController *)self;
        EmptyTipView *emptyTipView = [[EmptyTipView alloc]initWithFrame:CGRectMake(0,0,vcl.ctable.width,vcl.ctable.height) tipType:tip];
        [vcl.ctable addSubview:emptyTipView];
        [self setEmptyTipView:emptyTipView];
    }
}

-(void)hiddenEmptyTip
{
    EmptyTipView *tipView = [self emptyTipView];
    [tipView removeFromSuperview];
    [self setEmptyTipView:nil];
}

- (UIImageView*)findHairlineImageViewUnder:(UIView*)view
{
    if([view isKindOfClass:UIImageView.class] && view.bounds.size.height<=1.0) {
        return(UIImageView*)view;
    }
    for(UIView*subview in view.subviews) {
        UIImageView*imageView = [self findHairlineImageViewUnder:subview];
        if(imageView) {
            return imageView;
        }
    }
    return nil;
}
@end
